
import java.io.*;
import java.net.Socket;
import java.util.Scanner;
public class Client_Diffie_Hellman {

	    public static void main(String[] args) throws IOException 
	    {
		      long P,alpha,Xa,Ya,K,Yb;
		  	  Scanner sc=new Scanner(System.in);
			  System.out.println("Enter Alpha:");
			  alpha=sc.nextLong();
			  System.out.println("Enter P:");
			  P=sc.nextLong();
			  System.out.println("Enter Xa");
			  Xa=sc.nextLong();
		  	  Ya = (long) (Math.pow(alpha, Xa)%P);
		       Socket soc = new Socket("localhost", 4500);                     
		       OutputStream outputStream = soc.getOutputStream(); 
		       PrintWriter pwrite = new PrintWriter(outputStream, true);                    
		       InputStream inputStream = soc.getInputStream();
		       BufferedReader receiveRead = new BufferedReader(new InputStreamReader(inputStream));
		       System.out.println("Client is ready");
		       String receiveMessage;               
		          pwrite.println(Ya);       
		          pwrite.flush();                    
		          if((receiveMessage =receiveRead.readLine()) != null) 
		          {
		              System.out.println("Ya is "+Ya+" Yb is "+receiveMessage); 
		          }         
		        
		    		Yb = Long.parseLong(receiveMessage);	
		    		K = (long) (Math.pow(Yb, Xa)%P);
		  		System.out.println("Key is "+K);
		    }
	  
  }
        
/*
Server:
Enter Alpha:
13
Enter P:
23
Enter Xb
13
Server Available For Key Exchange
Ya is 1 Yb is 8
Key is 1

Client:
Enter Alpha:
13
Enter P:
23
Enter Xa
11
Client Available For Key Exchange
Ya is 1 Yb is 8
Key is 1
*/